<?php
/**
 * Project: ShopWorx- making your life easier
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * @Author Chris
 * @File login.php
 * @Version 1.0.0
 * @Copyright 2008 Shopworx (c)
 * @Year 2008
 * @access Public
 */

require_once 'common.php';


$count = 0;


class login extends Application
{

    function run()
    {
		global $db_url;
        $this->db = new DBAL($db_url);
        $email = $this->getRequestField('email');
        $password = $this->getRequestField('password');
        $emailLen = strlen($email);
        $passwdLen = strlen($password);

        $ref = $this->getRequestField('ref') . '.php';
        if ($ref == '.php') {
            $ref = 'index.php';
        }


        $this->debug("Login attempts : " . $this->getSessionField("SESSION_ATTEMPTS"));

        if ($this->is_authenticated()) {
            $this->debug("User already authenticated.");

            $this->debug("Redirecting to $ref.");
            header("location: $ref");
        } else
            if (strlen($email) < MIN_USERNAME_SIZE || strlen($password) < MIN_PASSWORD_SIZE) {
                $this->debug("Invalid Email or password.");
                $this->display_login();
                $_SESSION["SESSION_ATTEMPTS"] = $this->getSessionField("SESSION_ATTEMPTS") + 1;

            } else {
                $this->debug("Authenticate user $email with password $password");
                $auth = new Auth($this->db);
                if ($auth->Authenticate($email, $password) == true) {
                    $this->debug("User is successfully authenticated.");
                    $_SESSION["SESSION_USERNAME"] = $email;
                    $_SESSION["SESSION_PASSWORD"] = $password;
                    $_SESSION["SESSION_USER_ID"] = $auth->getUID();

                    header("Location : $ref");

                    $this->debug("Location $ref");

                    $this->debug("Redirect user to caller application at url > <a href = \"$ref\">$ref</a>.");
                } else {
                    $this->debug("User failed auhentication.");
                    $this->display_login();
                    $err_text = "Wrong username or password, Please try again.";
                    $_SESSION["SESSION_ATTEMPTS"] = $this->getSessionField("SESSION_ATTEMPTS") + 1;

                }
            }
    }

    function warn()
    {
        global $WARNING_URL;
        $this->debug("Came to warn the user $WARNING_URL");
        header("Location: $WARNING_URL");
    }

    function display_login()
    {
        global $email, $ref, $err_text;
        $ref = $this->getRequestField('ref') . '.php';
        if ($ref == '.php') {
            $ref = 'index.php';
        }
        if ($this->getSessionField("SESSION_ATTEMPTS") > MAX_LOGIN_ATTEMPTS) {
            $this->warn();
        }

        $this->debug("Display login dialog");
        $template = new Smarty();
        $template->template_dir = TEMPLATE_DIR;
        $template->cache_dir = TEMPLATE_CACHE_DIR;
        $template->compile_dir = TEMPLATE_COMPILE_DIR;
        $template->assign('SELF_PATH', SCRIPT_PATH);
        $template->assign('ATTEMPT', $this->getSessionField("SESSION_ATTEMPTS"));
        $template->assign('TODAY', date("M-d-Y h:i:s a"));
        $template->assign('TODAY_TS', time());
        $template->assign('SITE_NAME', SITE_NAME);
        $template->assign('PAGE', 'Sign In');
        $template->assign('Error', $err_text);
        $template->assign('TEMPLATE_STYLE', TEMPLATE_DIR . '/' . TEMPLATE_STYLE . '/' .
            STYLE_SHEET);
        $template->assign('USERNAME', $email);
        $template->assign('REDIRECT_URL', $ref);
        $template->assign('FORGOTTEN_PASSWORD_APP', PASSWORD_RESET_APP);
        $template->display("login.htm");
        return 1;
    }
    
    function is_authenticated()
    {
        return (!empty($_SESSION['SESSION_USERNAME'])) ? true : false;
    }

}

$login = new login(array('app_name' => 'Login',
                         'app_version' => '1.0.1',
                         'app_type' => 'WEB',
						 'app_db_url' => $db_url,
						 'app_auto_connect' => true,
                         'app_auto_authorize' => false,
						 'app_auto_chk_session' => false,
						 'app_debugger' => true));

$login->buffer_debugging();
$login->debug("<i><b>Server :</b> </i><font color=\"#000\">&ldquo;" . $_SERVER['SERVER_NAME'] .
    '&rdquo;</font>');
$login->debug("<i><b>Application version :</b> </i><font color=\"#000\">&ldquo;" .
    $login->get_version() . '&rdquo;</font>');
$login->debug("<i><b>App directory : </i></b><font color=\"#000\">" . FRAMEWORK_DIR .
    "</font>");
$login->run();
$login->dump_debuginfo();
